package jsyf.oa.dao.device;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.oa.entity.device.DeviceEntity;
import jsyf.oa.idao.BaseDao;
import jsyf.oa.ientity.IBaseEntity;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by LT on 2018/3/6.
 */
@Repository
public class DeviceDao extends BaseDao {

    public DeviceDao() {
        super.iBaseEntity = new DeviceEntity();
    }

    @Override
    public IBaseEntity convertJson2Entity(JSONObject json) {
        return JSON.toJavaObject(json, DeviceEntity.class);
    }

    public List<JSONObject> getDeviceList(String condition,Object[] params, int requestPage, int pageSize){
        StringBuffer sql=new StringBuffer();
        sql.append(" SELECT t_dvc_model.ID AS id,t_dvc_device_type.`NAME` AS deviceTypeName,");
        sql.append(" t_dvc_brand.BRAND_NAME AS brandName,");
        sql.append("  t_dvc_model.MODEL_NAME AS modelName,");
        sql.append(" t_dvc_model.LOWEST_PRICE AS price,");
        sql.append(" t_dvc_model.TON_CONFIG AS tonConfig,");
        sql.append(" t_dvc_model.REMARK AS remark,");
        sql.append(" t_dvc_model.DEL_FLAG as delFlag FROM");
        sql.append(" `t_dvc_model`");
        sql.append(" LEFT JOIN t_dvc_brand ON t_dvc_model.BRAND_ID = t_dvc_brand.ID\n");
        sql.append(" LEFT JOIN t_dvc_device_type ON t_dvc_device_type.ID=t_dvc_model.DEVICE_TYPE_ID");
        sql.append(" WHERE 1=1");
        sql.append(condition);
        sql.append(" GROUP BY t_dvc_model.ID");
        sql.append(" order by t_dvc_model.SORT_NO desc");
        super.appendPageSql(sql, requestPage, pageSize);
        List<JSONObject> jsonList = super.queryForJsonList(sql.toString(), params);
        return jsonList;
    }

    public List<JSONObject> getDeviceByCompanyId(String condition,Object[] params){
        StringBuffer sql=new StringBuffer();
        sql.append(" SELECT t_dvc_brand.BRAND_NAME AS brandName,t_dvc_brand.ID AS brandId\n");
        sql.append(" FROM\n");
        sql.append(" t_sys_company\n");
        sql.append(" LEFT JOIN t_sys_company_2_brand ON t_sys_company.ID = t_sys_company_2_brand.COMPANY_ID\n");
        sql.append("LEFT JOIN t_dvc_brand ON t_dvc_brand.ID = t_sys_company_2_brand.BRAND_ID\n");
        sql.append(" where 1=1");
        sql.append(condition);
        List<JSONObject> jsonList = super.queryForJsonList(sql.toString(), params);
        return jsonList;
    }
    public Integer getPageCount1(String condition, Object[] params) {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT count(DISTINCT(t_dvc_model.ID)) AS count");
        sql.append("  FROM");
        sql.append(" `t_dvc_model`");
        sql.append(" LEFT JOIN t_dvc_brand ON t_dvc_model.BRAND_ID = t_dvc_brand.ID\n");
        sql.append(" LEFT JOIN t_dvc_device_type ON t_dvc_device_type.ID=t_dvc_model.DEVICE_TYPE_ID");
        sql.append(" WHERE 1=1");
        sql.append(condition);
        String count = super.queryForString(sql.toString(), params);
        return Integer.parseInt(count);
    }
    public JSONObject getDeviceById(String id){
        StringBuffer sql=new StringBuffer();
        sql.append(" SELECT t_dvc_model.ID AS fkModelId,t_dvc_device_type.`NAME` AS deviceTypeName,");
        sql.append("  t_dvc_model.BRAND_ID as fkBrandId,");
        sql.append("  t_dvc_model.DEVICE_TYPE_ID as fkDeviceTypeId,");
        sql.append(" t_dvc_brand.BRAND_NAME AS brandName,");
        sql.append("  t_dvc_model.MODEL_NAME AS modelName,");
        sql.append(" t_dvc_model.LOWEST_PRICE AS price,");
        sql.append(" t_dvc_model.REMARK AS remark,");
        sql.append(" t_dvc_model.CONFIG AS config,");
        sql.append(" t_dvc_model.TON_CONFIG AS tonConfig,");
        sql.append(" t_dvc_model.DEL_FLAG as delFlag FROM");
        sql.append(" `t_dvc_model`");
        sql.append(" LEFT JOIN t_dvc_brand ON t_dvc_model.BRAND_ID = t_dvc_brand.ID\n");
        sql.append(" LEFT JOIN t_dvc_device_type ON t_dvc_device_type.ID=t_dvc_model.DEVICE_TYPE_ID");
        sql.append(" WHERE 1=1");
        sql.append(" AND t_dvc_model.ID=?");
        return super.queryForJsonObject(sql.toString(),new Object[]{id});
    }
}
